<template>
	<view>
		<view class="wx-page" v-if="iswx == true">
			<view class="wx-mask">
				<view class="wx-tip">
					<view class="tip-arrow">↗</view>
					<view class="tip-text">请点击右上角</view>
					<view class="tip-text">选择在浏览器中打开</view>
				</view>
			</view>
		</view>
		<view v-else class="login-page">
			<u-navbar leftIconColor="#ffffff" :autoBack="true" :placeholder="true" bgColor="rgba(255,255,255,0.0)">
				<view slot="center" class="nav-title">
					注册账号
				</view>
			</u-navbar>
			<view class="login-info">
				<view class="title">注册手机号</view>
				<view class="input">
					<u-input type="number" placeholder="请输入您的手机号" :maxlength="11" shape="circle" border="none"
						v-model="tel"></u-input>
				</view>
				<view class="title">验证码</view>
				<view class="input">
					<u-input placeholder="请输入验证码" v-model="code" border="none">
						<template slot="suffix">
							<u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒重新获取"></u-code>
							<u-button @tap="getCode" type="success" size="mini" color="rgba(0,0,0,0.0)"
								class="custom-btn"><text style="font-size: 26rpx;">{{tips}}</text></u-button>
						</template>
					</u-input>
				</view>
				<view class="title">请设置登录密码</view>
				<view class="input">
					<u-input placeholder="请设置您的登录密码" shape="circle" type="password" border="none"
						v-model="password"></u-input>
				</view>
				<view class="title">请设置支付密码</view>
				<view class="input">
					<u-input placeholder="请设置您的支付密码" shape="circle" type="number" border="none" v-model="pay_password"
						:maxlength="6"></u-input>
				</view>
				<view class="title">请输入邀请码</view>
				<view class="input">
					<u-input placeholder="请输入您的邀请码" shape="circle" type="text" border="none"
						v-model="invite_code"></u-input>
				</view>
				<view class="btn">
					<u-button style="color: #000;" size="large" type="primary" color="linear-gradient(255.89deg, #FFF0E3 6.94%, #D7B39A 87.16%)" text="注册"
						@click="goRegister"></u-button>
				</view>

			</view>
			<view class="btn-box">
				<view class="btn">
					<u-button style="color: #000;"  size="large" type="primary" color="linear-gradient(255.89deg, #FFF0E3 6.94%, #D7B39A 87.16%)" text="安卓app"
						@click="gourl(1)"></u-button>
				</view>
				<view class="btn">
					<u-button style="color: #000;"  size="large" type="primary" color="linear-gradient(255.89deg, #FFF0E3 6.94%, #D7B39A 87.16%)" text="苹果app"
						@click="gourl(2)"></u-button>
				</view>
			</view>
			<view class="bottom">
				<u-checkbox-group>
					<u-checkbox v-model="checked" @change="gochecked" shape="circle" active-color="#090B08"
						size="14px"></u-checkbox>
				</u-checkbox-group>
				<view>
					已阅读并同意<text @click="showAgreement = true">《注册协议》</text>
				</view>
			</view>
			<u-popup :show="showAgreement" round="10" mode="center" @close="showAgreement = false">
				<view class="agreement-content">
					<view class="agreement-header">
						<text class="agreement-title">注册协议</text>
						<u-icon name="close" size="24" @click="showAgreement = false"></u-icon>
					</view>
					<scroll-view scroll-y class="agreement-text">
						<rich-text :nodes="agreementContent"></rich-text>
					</scroll-view>
				</view>
			</u-popup>
		</view>
	</view>
</template>

<script>
	import CryptoJS from 'crypto-js';
	export default {
		data() {
			return {
				tel: '',
				code: '',
				password: '',
				checked: false,
				tips: '',
				pay_password: '',
				invite_code: '',
				iswx: true,
				showAgreement: false,
				agreementContent: '',
				urlbox: {}
			}
		},
		onLoad(e) {
			// 判断是否在微信浏览器
			const ua = window.navigator.userAgent.toLowerCase();
			this.iswx = ua.indexOf('micromessenger') !== -1;
			this.showAgreementModal()
			this.sendrender()
			if (e.code) {
				this.invite_code = e.code
			}
		},
		methods: {
			async showAgreementModal() {
				const res = await this.$Request({
					method: 'GET',
					url: '/api/login/reg_agreement'
				})

				if (res.code === 1) {
					this.agreementContent = res.data.content
				}
			},
			md5Encrypt(text) {
				return CryptoJS.MD5(text).toString();
			},
			sendrender() {
				this.$Request({
					method: 'POST',
					url: '/api/login/getdownload',
					data: {},
					ismsg: false,
				}).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						this.urlbox = res.data
					}
				})
			},
			gochecked(e) {
				this.checked = e
			},
			codeChange(text) {
				this.tips = text;
			},
			gourl(type) {
				if (type == 1) {
					window.open(this.urlbox.apkurl);
				}
				if (type == 2) {
					window.open(this.urlbox.iosurl);
				}
			},
			getCode() {
				if (this.tel == '') {
					uni.showToast({
						title: '手机号不可为空！',
						icon: 'none'
					})
					return
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					let params = {
						phone: this.tel
					}
					this.$Request({
						method: 'POST',
						url: '/api/login/sendRegister',
						data: params,
						ismsg: true,
					}).then(res => {
						uni.hideLoading()
						if (res.code == 1) {
							uni.$u.toast('验证码已发送');
							this.$refs.uCode.start();
						}
					})
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			goRegister() {
				const regex = /^(?=.*[a-zA-Z])(?=.*\d).{9,}$/;
				if (this.checked == false) {
					uni.showToast({
						title: '请仔细阅读并同意注册协议！',
						icon: 'none'
					})
					return
				}
				if (this.tel == '') {
					uni.showToast({
						title: '手机号不可为空！',
						icon: 'none'
					})
					return
				}
				if (regex.test(this.password) == false) {
					uni.showToast({
						title: '密码长度必须大于8位数并且包含字母和数组！',
						icon: 'none'
					})
					return
				}
				if (this.code == '') {
					uni.showToast({
						title: '验证码不可为空！',
						icon: 'none'
					})
					return
				}
				if (this.password == '') {
					uni.showToast({
						title: '密码不可为空！',
						icon: 'none'
					})
					return
				}
				// 检查支付密码是否为空
				if (this.pay_password === '') {
					uni.showToast({
						title: '支付密码不可为空！',
						icon: 'none'
					});
					return;
				}

				// 检查是否是6位纯数字
				if (!/^\d{6}$/.test(this.pay_password)) {
					uni.showToast({
						title: '支付密码必须是6位数字！',
						icon: 'none'
					});
					return;
				}
				if (this.invite_code == '') {
					uni.showToast({
						title: '邀请码不可为空！',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					mask: true
				})
				let params = {
					code: this.code,
					guid: this.invite_code,
					password: this.md5Encrypt(this.password),
					pay_pass: this.md5Encrypt(this.pay_password),
					phone: this.tel
				}
				this.$Request({
					method: 'POST',
					url: '/api/login/register',
					data: params,
					ismsg: true,
				}).then(res => {
					console.log(res)
					uni.hideLoading()
					if (res.code == 1) {
						this.tel = ''
						this.code = ''
						this.password = ''
						this.pay_password = ''
						this.code = ''
					}
				})
			}
		}
	}
</script>

<style>
	.custom-btn {
		color: #52ECFA !important;
	}
	page{
		background-color: #000000;
	}
</style>
<style scoped lang="scss">
	.wx-page {
		height: 100vh;
		background-image: url('../../static/home/wx.png');
		background-repeat: no-repeat;
		background-size: 100% 100%;
		position: relative;

		.wx-mask {
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			bottom: 0;
			background-color: rgba(0, 0, 0, 0.5);
			display: flex;
			justify-content: flex-end;
			padding-top: 40rpx;
			padding-right: 40rpx;

			.wx-tip {
				text-align: center;
				color: #fff;
				margin-top: 80rpx;
				margin-right: 40rpx;

				.tip-arrow {
					font-size: 60rpx;
					margin-bottom: 20rpx;
				}

				.tip-text {
					font-size: 32rpx;
					line-height: 1.6;
				}
			}
		}
	}

	.login-page {
		height: 100vh;
		background-image: url('../../static/login/login-bg.png');
		background-repeat: no-repeat;
		background-size: 100% 593px;
		.nav-title{
			color: #fff;
		}
		.login-info {
			width: 95%;
			margin: 0 auto;
			margin-top: 30rpx;
			padding: 30rpx;
			.title{
				color: #ffffff;
				margin-bottom: 20rpx;
				font-size: 26rpx;
			}
			.btn {
				margin-top: 30rpx;
			}

			.input {
				background-color: #1A1A1B;
				margin-bottom: 30rpx;
				padding: 30rpx;
				border-radius: 30rpx;
			}
		}

		.btn-box {
			padding: 30rpx 50rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.btn {
				width: 45%;
			}
		}

		.bottom {
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 26rpx;
			margin-top: 20rpx;
			color: #666666;
			text {
				color: #FFFFFF;
			}
		}
		.agreement-content {
			width: 80vw;
			height: 50vh;
			overflow: hidden;
			padding: 20rpx;

			.agreement-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 20rpx;
				padding-bottom: 20rpx;
				border-bottom: 1px solid #666666;

				.agreement-title {
					font-size: 32rpx;
					font-weight: bold;
					color: #333;
				}
			}

			.agreement-text {
				height: 40vh;
				overflow: hidden;
				font-size: 28rpx;
				color: #666;
				line-height: 1.6;
				padding-bottom: 30rpx;
			}
		}

	}
</style>